/**
 * 
 */
package com;

import com.database.databaseElement;
import java.util.HashMap;

import misc.utility;

import database.databaseInteraction;
import database.database_auth;
import com.database.databaseElement;


import database.database_auth;

/**
 * @author user
 *
 */
public class chatSession extends databaseElement {

	public utility utility_inst = new utility();
	
//	  `toUserID` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'if this message is addressed to a user than this is his ID, otherwise it is 0',
	private String toUserID = "";
     
    public String gettoUserID() 
    { 
    	return toUserID;
    }
    public void settoUserID( String toUserID ) 
    {
    		this.toUserID = toUserID;
    		
    		if(utility_inst.not_empty(this.toUserID) && utility_inst.not_empty(this.fromUserID))
    		{
    			this.ChatSessionID = findChatSessionID(toUserID,fromUserID);
    		}
    }

//	  `toGroupID` bigint(20) NOT NULL DEFAULT '0' COMMENT 'if this message is addressed to a group than this is his ID, otherwise it is 0',
	public String toGroupID = "0";
//	  `toDiscussionID` bigint(20) NOT NULL DEFAULT '0' COMMENT 'if this message is addressed to a discussion than this is his ID, otherwise it is 0',
	public String toDiscussionID = "0";
//	  `toProjectID` bigint(20) NOT NULL DEFAULT '0' COMMENT 'if this message is addressed to a project than this is his ID, otherwise it is 0',
	public String toProjectID = "0";
//    `fromUserID` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'ID of the sender of the message',

	private String fromUserID = "";
    public String getfromUserID() 
    { 
    	return fromUserID;
    }
    public void setfromUserID( String fromUserID ) 
    { 
		if(utility_inst.not_empty(this.toUserID) && utility_inst.not_empty(this.fromUserID))
		{
			this.ChatSessionID = findChatSessionID(this.toUserID,this.fromUserID);
		}

    	this.fromUserID = fromUserID; 
    }

	public String text = "";
	public String ChatSessionID = "";
	public String subject = "";
	public discussion DISCUSSION = null; // contains discussionParentID, toDiscussionID == ID of the discussion
	public String TimeStarted = ""; // timeStamp in MS since 1.1.1970 when the discussion / message was started
	public String TimeFinished = ""; // timeStamp in MS since 1.1.1970 when the discussion / message was started
	public String TimeLastMessage = ""; // timeStamp in MS since 1.1.1970 when last message was added (to see how old the discussion is)
	public String ChatMessageCount = "0"; // amounts of messages in that chat
	public String Priority = ""; // priority, high,middle,low
	public String status = ""; // read, unread, deleted
//	   `spam` bigint(20) NOT NULL DEFAULT '0' COMMENT 'defines the spam score/ranking of a message, it should be setable in the options how many people need to mark a message as spam to treat the message as spam',
	public String spam = ""; // read, unread, deleted, spam

	databaseInteraction DatabaseConnection = new databaseInteraction();
	
	/**
	 * @param database_auth_inst
	 */
	public chatSession(database_auth database_auth_inst)
	{
		super(database_auth_inst, "chat", "ChatSessionID");
	}
	
    /** checks in database.table "chat" if there is allready a ChatSessionID with fromUserID:toUserID or toUserID:fromUserID
     * if none is found just create it
     * **/
    public String findChatSessionID(String toUserID, String fromUserID)
    {
    	String ChatSessionID = "";
		// check if the discussion already exists, if not create it
		String question = "SELECT * FROM `chat` WHERE `ChatSessionID` = '"+toUserID+":"+fromUserID+"' OR  `ChatSessionID` = '"+fromUserID+":"+toUserID+"';";
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
		
		int LENGTH = DatabaseConnection.answerJAVA.size();
		
		if(LENGTH == 0)
		{
			ChatSessionID = fromUserID+":"+toUserID;
		}

		status = DatabaseConnection.status;
		
		return ChatSessionID;
    }
}